<!DOCTYPE html>
<html>
<head>
<title>ANTHONY BALMEO : PROJECT 1A</title>
</head>

<body>
<h2>PHP Calculator</h2>
<p>By: Anthony Balmeo</p>
<form name="input" action="" method="get">
Enter Values: <input type="text" name="calculate" />
<input type="submit" value="Submit" />
</form>

<?php

// CHECK_SYNTAX EXPRESSION FUNCTION TO EVALUATE EQUATION
function check_syntax($val) {

    // VARIABLES DEFINED
    $number = "\d+(\.\d+)?";  // CHECK FOR NUMBERS
    $sign   = "[+-]?($number)"; // DETERMINES SIGN OF NUMBER
    $oper     = "[+*/-]"; // OPERATORS
    $exp  = "$sign($oper$sign)*"; // EXPRESSION/EQUATION 

    return preg_match("~^$exp$~", $val); //RETURN TRUE/FALSE FOR EPRESSION
}


if(isset($_GET["calculate"]) && strlen($_GET["calculate"]) != 0){

    echo "<h3>Answer</h3>";

	// GET VALUE FROM TEXT AREA
    $input = $_GET["calculate"];
	
	// PASS INPUT INTO FUNCTION
    if(check_syntax($input)){
    // RETURN ANSWER
        echo "$input = ".eval("return @($input);");
        
        
    }else{
        echo "Input Equation Correctly $input.";
    }

}

?>


</body>

</html>

